package com.zhonghui.common.util;

import com.zhonghui.common.eshopUtil.CateDTO;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class TreeUtil {
    public TreeUtil() {
    }

    public static List<CateDTO> list2TreeConverter(List<CateDTO> list, int parentId) {
        List<CateDTO> returnList = new ArrayList();
        Iterator var3 = list.iterator();

        while(var3.hasNext()) {
            CateDTO res = (CateDTO)var3.next();
            if (res.getPid() == (long)parentId) {
                recursionFn(list, res);
                returnList.add(res);
            }
        }

        return returnList;
    }

    public static void recursionFn(List<CateDTO> list, CateDTO t) {
        List<CateDTO> childsList = getChildList(list, t);
        t.setChildren(childsList);
        Iterator var3 = childsList.iterator();

        while(var3.hasNext()) {
            CateDTO nextChild = (CateDTO)var3.next();
            if (!CollectionUtils.isEmpty(childsList)) {
                recursionFn(list, nextChild);
            }
        }

    }

    public static List<CateDTO> getChildList(List<CateDTO> list, CateDTO t) {
        List<CateDTO> childsList = new ArrayList();
        Iterator var3 = list.iterator();

        while(var3.hasNext()) {
            CateDTO t1 = (CateDTO)var3.next();
            if (t1.getPid().equals(t.getId())) {
                childsList.add(t1);
            }
        }

        return childsList;
    }

    public static boolean hasChild(List<CateDTO> list, CateDTO t) {
        return getChildList(list, t).size() > 0;
    }
}
